package org.example.controller;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.util.List;

public class PhantomjsTest {
    public static void main(String[] args) throws InterruptedException {
//        1.安装phantomjs 解压
//        2.添加selenium jar包和phantomjs驱动
//        3.创建phantomjs的配置 设置必要参数
        DesiredCapabilities dcaps=new DesiredCapabilities();
        dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
                "H:\\phantomjs\\bin\\phantomjs.exe");
//        4创建一个RemoteWebDriver对象，相当于启动浏览器
        RemoteWebDriver remoteWebDriver=new PhantomJSDriver(dcaps);
//        5.使用Driver对象访问一个路径
        remoteWebDriver.get("https://uland.taobao.com/sem/tbsearch?refpid=mm_26632360_8858797_29866178&keyword=%E6%89%8B%E6%9C%BA&clk1=bcaf20696ea10621e86fa013ca79c82f&upsId=bcaf20696ea10621e86fa013ca79c82f&spm=a2e0b.20350158.search.1&pid=mm_26632360_8858797_29866178&union_lens=recoveryid%3A201_33.5.38.198_59276710_1698236887518%3Bprepvid%3A201_33.5.38.198_59276710_1698236887518");
//          执行语句使页面滚动到最下面
        remoteWebDriver.executeScript("window.scrollTo(0,document.body.scrollHeight -300)");
//          延时两秒
        Thread.sleep(2000);
        //        6.从driver对象中取出浏览器渲染的结果
        List<WebElement> list= remoteWebDriver.findElementsByCssSelector("li.pc-items-item");
        System.out.println(list.size());
//        7.关闭浏览器
        remoteWebDriver.close();
    }
}
